package com.zkw.group.controller;

import com.alibaba.fastjson.JSON;
import com.zkw.group.entity.ProfesTerm;
import com.zkw.group.entity.QueryInfo;
import com.zkw.group.service.ProfesTermService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(description = "管理学期信息")
@CrossOrigin
@RestController
@RequestMapping("/ProfesTerms")
public class ProfesTermController {

    @Autowired
    private ProfesTermService profesTermService;

    @ApiOperation(value = "分页查询学期列表")
    @GetMapping("/AllProfesTerm")
    public String getAllProfesTermList(QueryInfo queryInfo) {
        int numbers = profesTermService.getProfesTermCounts(queryInfo.getQuery());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<ProfesTerm> profesTerms = profesTermService.getAllProfesTermList(queryInfo.getQuery(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data", profesTerms);
        return JSON.toJSONString(res);
    }

    @ApiOperation(value = "添加学期")
    @PostMapping("/ProfesTerm")
    public String addProfesTerm(@RequestBody ProfesTerm profesTerm) {
        int result;
        try{
            result = profesTermService.addProfesTerm(profesTerm);
        }
        catch(Exception e) {
            result = 0;
        }
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "删除指定学期")
    @DeleteMapping("/ProfesTerm")
    public String deleteProfesTerm(String termname) {
        int result = profesTermService.deleteProfesTerm(termname);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "查询指定学期信息")
    @GetMapping("/ProfesTermMessage")
    public String getUpdateProfesTerm(String termname) {
        ProfesTerm updateProfesProfesTerm = profesTermService.getUpdateProfesTerm(termname);
        String users_json = JSON.toJSONString(updateProfesProfesTerm);
        return users_json;
    }

    @ApiOperation(value = "更新指定学期信息")
    @PutMapping("/ProfesTerm")
    public String updateProfesTerm(@RequestBody ProfesTerm profesTerm) {
        int result = profesTermService.updateProfesTerm(profesTerm);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }
}
